using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI.WebControls;
using Framework.Library.Base.Base;
using Framework.Library.Base.DataAccess;
using Framework.Library.Base.Extensions;
using Framework.Library.Base.Helper;
using Framework.Library.Core.Config;
using Framework.Library.Core.Utils.GlobalHelper;
using Framework.Library.Core.Utils.URLBuilder;
using Framework.Library.Data.DAL;
using Framework.Library.Data.DAO;

namespace Framework.Portal.Layouts.Admin.Modules.NavPanels
{
    public partial class NavPanel : BaseControl
    {
        private string WORKING_PORTAL
        {
            get
            {
                return cfg.PrefixMember.VALUE + "WORKING_PORTAL";
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                lblCurrentUser.Text = Session.GetUser().DISPLAYNAME;
                if (Session.GetUser().TYPE == (int)UserType.govn)
                    lblCurrentUser.NavigateUrl = "http://my.go.vn/?id=" + currentUserId;
                else
                    lblCurrentUser.NavigateUrl = "/Default.aspx?p=3&l=vi-vn&m=30&t=1&uid=" + Session.GetUser().USERID;
                if (Configurations.MULTILANGUAGE)
                {
                    List<PortalLanguage> dtLang = ManagerFactory.DAOEntity.GetList<PortalLanguage>(new PortalLanguage());
                    if (dtLang != null)
                    {
                        rptLanguage.DataSource = dtLang;
                        rptLanguage.ItemDataBound += rptLanguage_ItemDataBound;
                        rptLanguage.DataBind();
                    }
                    else
                        rptLanguage.Visible = false;
                }
                else
                    rptLanguage.Visible = false;
                var dtPortals = new List<PortalPortal>();
                if (Session.GetUser().ISSUPERADMIN > 0)
                    dtPortals = ManagerFactory.DAOEntity.GetList<PortalPortal>(new PortalPortal() { PORTAL_PARENTID = 0 });
                else
                    dtPortals = ManagerFactory.PortalPortalManager.GetPortalsByParentId(Session.GetUser().USERID, 0);
                if (dtPortals != null)
                {
                    PortalAccess.LoadPortals(dtPortals, dropPortal.Items);
                    if (Session.WorkingPortal() != null)
                        dropPortal.SelectedValue = Session.WorkingPortal().PORTALID.ToString();
                    else
                        HttpContext.Current.Session.Add(WORKING_PORTAL, ManagerFactory.DAOEntity.GetInfo(new PortalPortal() { PORTALID = Convert.ToInt32(dropPortal.SelectedValue) }));
                }
                btnLogout.Attributes.Add("onclick",
                                         "javascript:" + Localization.GetText("MESSAGE", "MSG_LOGOUT_CONFIRM"));
                hplChangePass.NavigateUrl = cfg.MenuPath(3, 30, 3, string.Empty) + "&uid=" + Session.GetUser().USERID;
                lblEDITPORTAL.NavigateUrl = cfg.MenuPath(3, 28) + "&edit=" + dropPortal.SelectedValue;
                lblNEWPORTAL.NavigateUrl = cfg.MenuPath(3, 28);
            }
        }

        protected void btnLogout_Click(object sender, EventArgs e)
        {
            LoginHelper.LogoutUser();
            //LoginHelper.DoLogout();
            //Response.Redirect(Configurations.ROOTDIR + "Default.aspx?p=" + PortalId);
        }

        protected void dropPortal_SelectedIndexChanged(object sender, EventArgs e)
        {
            HttpContext.Current.Session.Remove(WORKING_PORTAL);
            HttpContext.Current.Session.Add(WORKING_PORTAL, ManagerFactory.DAOEntity.GetInfo(new PortalPortal() { PORTALID = Convert.ToInt32(dropPortal.SelectedValue) }));
            Response.Redirect(Request.RawUrl);
        }

        protected void rptLanguage_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var currow = (PortalLanguage) e.Item.DataItem;
                var lbalias = (LinkButton) e.Item.FindControl("hplLanguage");
                lbalias.Text = (Convert.ToString(currow.LANGUAGE_NAME));
                lbalias.CommandName = "ChangeLanguage";
                if (currow.ID == Configurations.LANGID)
                {
                    lbalias.CssClass = "LinkActive";
                }
                lbalias.CommandArgument = currow.ID.ToString();
            }
        }

        protected void rptLanguage_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "ChangeLanguage")
            {
                var urlParser = new SimpleURLParameterParser(Request.RawUrl);
                var lstUrl = new Dictionary<string, string>();
                lstUrl.Add("l", e.CommandArgument.ToString());
                Response.Redirect(urlParser.CreateQueryString(lstUrl));
            }
        }
    }
}